package com.wang.users.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.util.BeanUtil;
import com.wang.users.mapper.AtRbacAdminsMapper;
import com.wang.users.pojo.AtRbacAdmins;
import com.wang.users.pojo.AtRbacAdminsQuery;
import com.wang.users.pojo.AtUsersDescQuery;
import com.wang.users.pojo.AtRbacAdmins;
import com.wang.users.pojo.AtRbacAdminsQuery;
import com.wang.users.service.AtRbacAdminsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 针对表【at_demo_dynasty(示例_朝代表)】的数据库操作Service实现
 *
 * @author ZjxMi
 */
@Service
public class AtRbacAdminsServiceImpl extends ServiceImpl<AtRbacAdminsMapper, AtRbacAdmins>
        implements AtRbacAdminsService {
    @Autowired
    private BeanUtil beanUtil;

    /**
     * model2: jsp --> servlet -->service --> dao --> db, 我们自己封装了一个 PageInfoUtil;
     * MybatisPlus: 不再需要调用 PageInfoUtil,老代码改造的时候是为了整合代码,如果没有老代码,直接写的新代码,采用下面的方式来搞
     *
     * @param page
     * @param query 查询对象
     * @return
     */
    @Override
    public List<AtRbacAdmins> findList(IPage<AtRbacAdmins> page, AtRbacAdminsQuery query) {
        return this.baseMapper.findList(page, query);
    }

    @Override
    public AtRbacAdmins findOne(AtRbacAdminsQuery query) {
        return this.baseMapper.findOne(query);
    }

    @Override
    public int deleteBatch(AtRbacAdminsQuery query) {
        return this.baseMapper.deleteBatch(query);
    }

    @Override
    public int updateBatch(AtRbacAdminsQuery query) {
        return this.baseMapper.updateBatch(query);
    }

    /**
     * 对数据额外处理(非用户填写的数据)
     * @param entity
     */
    private void proccedUpdateData(AtRbacAdmins entity){
        if (Objects.isNull(entity.getCreateTime())) {
            entity.setCreateTime(new Date());
        }
        entity.setUpdateTime(new Date());
        /* 判断对象是否为空 */
        if (Objects.isNull(entity.getPubTime())) {
            entity.setPubTime(new Date());
        }
        /* 处理邮件,邮箱不能重复 */
        AtRbacAdminsQuery paramQuery = new AtRbacAdminsQuery();
        AtRbacAdmins paramEntity = paramQuery.getEntity();
        paramEntity.setEmail(entity.getEmail());
        AtRbacAdmins resultDb = this.findOne(paramQuery);
        /* 什么要判断邮箱重复;
         * 邮箱必须查询到,并且id和当前的id不一样
         * emailExistsFlag:false:表示邮箱存在,请更换邮箱(验证不通过;
         * true:验证通过
         *
         * 保存的时候如何验证:只要查询出邮箱,直接不让保存
         * 更新的时候: 邮箱得存在,并且数据库中查询的id和当前对象的id得不一样才行
         *  */
        boolean emailExistsFlag = true;
        if (Objects.isNull(entity.getId())) {
            /* 说明是保存 */
            if (Objects.nonNull(resultDb)) {
                emailExistsFlag = false;
            }
        } else {
            if (Objects.nonNull(resultDb)) {
                /* 更新 */
                emailExistsFlag = resultDb.getId().equalsIgnoreCase(entity.getId());
            }
        }
        /* 邮箱重复,不能再往下进行,需要抛出异常 */
        Assert.isTrue(emailExistsFlag, "邮箱重复,请更换邮箱");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(AtRbacAdmins entity) {
        this.proccedUpdateData(entity);
        /* 新增加的记录默认未删除 */
        entity.setDelFlag(BasePojoEnum.DELFLAG_YES.getCode());
        entity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(AtRbacAdmins entity) {
        /* 先根据id查询数据库的内容; */
        AtRbacAdminsQuery paramQuery = new AtRbacAdminsQuery();
        AtRbacAdmins entityParam = paramQuery.getEntity();
        entityParam.setId(entity.getId());
        /* 数据库中查询的结果 */
        AtRbacAdmins oneDbResult = this.findOne(paramQuery);
        /*
         * 源对象和目标对象是一个类型(10个字段)
         * 源对象有5个字段有值,目标对象是10个有值
         * 把源对象中的5个字段有值,赋值到目标对象中.(覆盖)
         * */
        oneDbResult = (AtRbacAdmins) this.beanUtil.copy(entity, oneDbResult);
        this.proccedUpdateData(oneDbResult);
        return super.updateById(oneDbResult);
    }
}